package cn.snmp.mutual.order;

import java.util.HashMap;

import cn.snmp.mutual.nms.SnmpVarBind;
import cn.snmp.mutual.data.ObjService;

/**
 * @author dengkn
 * AbstractVarBindOrder 系统默认下发排序
 */
public class EqualsInstanceVarBindOrder extends AbstractVarBindOrder {

    HashMap<Integer, SnmpVarBind> varBindOrderMap = new HashMap<Integer, SnmpVarBind>();

    /**
     * @param varBinds
     * @return
     * @see
     */
    public SnmpVarBind[] order(SnmpVarBind[] varBinds) {

		/*ObjService snmpParamObj = (ObjService) super.snmp_condition.clone();
		ObjService rowSet = snmpParamObj.objectAt("RowSet", 0);*/

        if (varBinds.length == 1)
            return varBinds;

        ObjService mibTableInfo = (ObjService) super.mibTable.clone();
        int length = varBinds.length;
        SnmpVarBind[] newVars = new SnmpVarBind[length];

        int max = 0;//

        for (int i = 0; i < length; i++) {
            String oid = varBinds[i].getObjectID().toString();
            for (int j = 0, size = mibTableInfo.objectSize("MibNode"); j < size; j++) {
                ObjService mibNodeObj = mibTableInfo.objectAt("MibNode", j);
                String mibNodeOid = mibNodeObj.getStringValue("OID") + ".";
                int varBindIndex = mibNodeObj.getIntValue("VarBindIndex");
                max = Math.max(varBindIndex, max);
                if (varBindIndex != 0) {
                    if (oid.startsWith(mibNodeOid)) {
                        varBindOrderMap.put(varBindIndex, varBinds[i]);
                        System.out.println(varBindIndex + "==" + varBinds[i]);
                        break;
                    }
                }
            }
        }

        int count = 0;
        for (int i = 0; i <= max; i++) {
            SnmpVarBind val = varBindOrderMap.get(i);
            if (val == null)
                continue;
            newVars[count++] = varBindOrderMap.get(i);
        }
        return newVars;

    }

}
